<template>
    <div class="login-wrap">
        <div class="ms-login">
            <div class="ms-title">后台管理系统</div>
            <el-form :model="param" :rules="rules" class="ms-content" label-width="0px" ref="login">
                <el-form-item prop="username">
                    <el-input placeholder="请输入您的电话" v-model="param.username">
                        <el-button icon="el-icon-lx-people" slot="prepend"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input
                            @keyup.enter.native="submitForm()"
                            placeholder="请输入密码"
                            type="password"
                            v-model="param.password"
                    >
                        <el-button icon="el-icon-lx-lock" slot="prepend"></el-button>
                        f
                    </el-input>
                </el-form-item>
                <div class="login-btn">
                    <el-button @click="submitForm()" type="primary">登录</el-button>
                </div>
                <p class="login-tips" v-if="">Tips : 用户名和密码随便填。</p>
            </el-form>
        </div>
    </div>
</template>

<script>
    export default {

        data: function() {
            return {
                param: {
                    username: '13388888888',
                    password: '123abc'
                },
                rules: {
                    // 验证用户名是否合法
                    username: [
                        { required: true, message: '请输入登录电话号码', trigger: 'blur' },
                        { min: 3, max: 11, message: '长度在 3 到 10 个字符', trigger: 'blur' }
                    ],
                    // 验证密码是否合法
                    password: [
                        { required: true, message: '请输入登录密码', trigger: 'blur' },
                        { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
                    ]
                }
            };
        },
        methods: {
            submitForm() {
                var th = this;
                this.$refs.login.validate(valid => {
                    if (valid) {
                        this.$axios.get('user/login', {
                            params: {
                                username: this.param.username,
                                password: this.param.password
                            }
                        }).then(function(response) {

                            console.log(response);
                            if (response.data != '') {
                                th.$message.success('登录成功');
                                console.log("response.data-------------------:");
                                console.log(response.data);
                                /*localStorage.setItem('ms_username', th.param.username);
                                localStorage.setItem('dep_name', response.data.dep_name);
                                localStorage.setItem('staff_name', response.data.staff_name);
                                localStorage.setItem('dep_id', response.data.dep_id);*/
                                localStorage.setItem('ms_username', th.param.username);
                                window.sessionStorage.setItem('ms_username', th.param.username);
                                window.sessionStorage.setItem('dep_name', response.data.dep_name);
                                window.sessionStorage.setItem('staff_name', response.data.staff_name);
                                window.sessionStorage.setItem('dep_id', response.data.dep_id);
                                window.sessionStorage.setItem('job_number', response.data.job_number);
                                window.sessionStorage.setItem('userlogin_time', response.data.userlogin_time);
                                window.sessionStorage.setItem('userlogin_address', response.data.userlogin_address);
                                th.$router.push('/');
                            } else {
                                th.$message.error('账号或者密码错误');
                                console.log('error submit!!');
                                return false;
                            }
                        }).catch(function(error) {
                            console.log(error);
                        });

                    } else {
                        th.$message.error('请输入账号和密码');
                        console.log('error submit!!');
                        return false;
                    }
                });
            }
        }
    };
</script>

<style scoped>
    .login-wrap {
        position: relative;
        width: 100%;
        height: 100%;
        background-image: url(../../assets/img/login-bg.jpg);
        background-size: 100%;
    }

    .ms-title {
        width: 100%;
        line-height: 50px;
        text-align: center;
        font-size: 20px;
        color: #fff;
        border-bottom: 1px solid #ddd;
    }

    .ms-login {
        position: absolute;
        left: 50%;
        top: 50%;
        width: 350px;
        margin: -190px 0 0 -175px;
        border-radius: 5px;
        background: rgba(255, 255, 255, 0.3);
        overflow: hidden;
    }

    .ms-content {
        padding: 30px 30px;
    }

    .login-btn {
        text-align: center;
    }

    .login-btn button {
        width: 100%;
        height: 36px;
        margin-bottom: 10px;
    }

    .login-tips {
        font-size: 12px;
        line-height: 30px;
        color: #fff;
    }
</style>